package com.wsc.wisdomchina.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.HoveringScrollview;
import com.stx.xhb.mylibrary.XBanner;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.AdministrationTeenagersBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

public class CareTeenagersActivity extends BaseActivity implements View.OnClickListener, ContributorView, XBanner.XBannerAdapter {

    private Context context;
    private ContributorPresenter contributorPresenter;
    private XBanner fragment_lyxq_xBanner;//轮播
    private TextView fragment_lyxq_title;//标题
    private TextView fragment_lyxq_name;
    private ImageView fragment_lyxq_back;//返回按钮
    private LinearLayout fragment_lyxq_boda_phone;//拨打联系电话
    private String cidNumber;
    private final String BUILDINGDETAILFLAG = "BUILDINGDETAILFLAG";
    private AdministrationTeenagersBean floorInfoDetailBean;
    private TextView fragment_lyxq_intro;
    private WebView webview;
    private LinearLayout ll_mine_nav;
    private LinearLayout ll_mine_nav_replace;
    private RelativeLayout layout_nav_back;
    private HoveringScrollview refreshscrollView;
    private int layout_headTop;
    private LinearLayout ll_item;
    private TextView tv_headtitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.careteenager_layout);
        context = this;
        initView();
        cidNumber = getIntent().getStringExtra("cid");
        contributorPresenter = new ContributorPresenter(this, context);
        contributorPresenter.getAdministrationTeenagers(cidNumber, BUILDINGDETAILFLAG);
        refreshscrollView.setOnScrollListener(new HoveringScrollview.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                layout_headTop = ll_mine_nav_replace.getTop() + context.getResources().getDimensionPixelOffset(R.dimen.fivedp);;
                int commentTop = ll_item.getTop()/2;
                if (scrollY > layout_headTop) {
                    if (layout_nav_back.getParent() != ll_mine_nav) {
                        ll_mine_nav_replace.removeView(layout_nav_back);
                        ll_mine_nav.addView(layout_nav_back);
                        tv_headtitle.setText("关心青少年");
                    }
                } else {

                    if (layout_nav_back.getParent() != ll_mine_nav_replace) {
                        ll_mine_nav.removeView(layout_nav_back);
                        ll_mine_nav_replace.addView(layout_nav_back);
                        tv_headtitle.setText("关心青少年");
                    }
                }

                int alph = (int) (Tools.div(255, commentTop) * scrollY);
                if (alph <= 255) {
                    tv_headtitle.setTextColor(Color.argb(alph, 255, 255, 255));
                    fragment_lyxq_back.setAlpha(alph);
                    layout_nav_back.setAlpha((float) Tools.div((Tools.div(255, commentTop) * scrollY),255));
                } else {
                    tv_headtitle.setTextColor(Color.argb(255, 255, 255, 255));
                    fragment_lyxq_back.setAlpha(255);
                    layout_nav_back.setAlpha(1);
                }
            }
        });
    }

    private void initView() {
        fragment_lyxq_title = (TextView) findViewById(R.id.fragment_lyxq_title);
        fragment_lyxq_boda_phone = (LinearLayout) findViewById(R.id.fragment_lyxq_boda_phone);
        fragment_lyxq_boda_phone.setOnClickListener(this);
        fragment_lyxq_back = (ImageView) findViewById(R.id.fragment_lyxq_back);
        fragment_lyxq_back.setOnClickListener(this);
        fragment_lyxq_xBanner = (XBanner) findViewById(R.id.fragment_lyxq_xBanner);
        fragment_lyxq_name=(TextView) findViewById(R.id.fragment_lyxq_name);
        fragment_lyxq_intro=(TextView) findViewById(R.id.fragment_lyxq_intro);
        webview=(WebView) findViewById(R.id.webview);
        ll_mine_nav = (LinearLayout)findViewById(R.id.ll_mine_nav);
        ll_mine_nav_replace = (LinearLayout)findViewById(R.id.ll_mine_nav_replace);
        layout_nav_back = (RelativeLayout) findViewById(R.id.layout_nav_back);
        layout_nav_back.setAlpha(0);
        refreshscrollView = (HoveringScrollview) findViewById(R.id.mineScrollView);
        ll_item = (LinearLayout) findViewById(R.id.ll_item);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_lyxq_boda_phone:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + floorInfoDetailBean.getPhone());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.fragment_lyxq_back:
                finish();
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(BUILDINGDETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    floorInfoDetailBean = JsonTools.jsonStringToObject(jsonMessage, AdministrationTeenagersBean.class);
                    if (floorInfoDetailBean != null) {
                        fragment_lyxq_title.setText(floorInfoDetailBean.getTitle());
                        fragment_lyxq_intro.setText(floorInfoDetailBean.getIntro());
                        fragment_lyxq_xBanner.setData(floorInfoDetailBean.getBanners(), null);
                        fragment_lyxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                        fragment_lyxq_xBanner.setmAdapter(this);
                        String Url = Constant.Head_string + floorInfoDetailBean.getContent() + Constant.foot_string;//URL编码数据
                        webview.getSettings().setDefaultTextEncodingName("UTF-8");
                        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                        webview.getSettings().setLoadWithOverviewMode(false);
                        webview.loadData(Url, "text/html; charset=UTF-8", null);
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void loadBanner(XBanner banner, Object model, View view, int position) {
        try{
            Glide.with(context).load(floorInfoDetailBean.getBanners().get(position)).into((ImageView) view);
        }catch (Exception e)
        {

        }

    }

}
